<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList, java.util.List"%>
<%@page pageEncoding="utf-8"%>
<%@page import="java.sql.*, com.liangjie.bbs.*"%>
<%@include file="./_sessionCheck.jsp"%>

<%!private void delete(Connection conn, int id, boolean isLeaf) {

		//删除子贴
		if (!isLeaf) {
			String sql = "select * from article where pid = " + id;
			Statement stmt = DB.getStmt(conn);
			ResultSet rs = DB.executeQuerey(stmt, sql);
			try {
				while (rs.next()) {
					delete(conn, rs.getInt("id"), rs.getInt("isleaf") == 0);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DB.close(rs);
				DB.close(stmt);
			}
		}

		//删除本帖
		try {
			Statement delStmt = DB.getStmt(conn);
			delStmt.executeUpdate("delete from article where id = " + id);
			DB.close(delStmt);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}%>

<%
	Connection conn = DB.getConn();
	boolean autoCommit = conn.getAutoCommit();
	conn.setAutoCommit(false);

	int id = Integer.parseInt(request.getParameter("id"));
	boolean isLeaf = Boolean.parseBoolean(request
			.getParameter("isLeaf"));
	int pId = Integer.parseInt(request.getParameter("pId"));
	delete(conn, id, isLeaf);

	//删除后无子贴，设为叶子节点
	Statement stmt = DB.getStmt(conn);
	String sql = "select count(*) from article where pid = " + pId;
	ResultSet rs = DB.executeQuerey(stmt, sql);

	rs.next();
	if (rs.getInt(1) <= 0) {
		Statement s = DB.getStmt(conn);
		s.executeUpdate("update article set isleaf = 0 where id ="
				+ pId);
		DB.close(s);
	}

	conn.commit();
	conn.setAutoCommit(autoCommit);

	DB.close(rs);
	DB.close(stmt);
	DB.close(conn);

	response.sendRedirect("./article.jsp");
%>